package net.toocruel.iqismart.web.controller.core;

import net.toocruel.iqismart.dao.core.AnswerRepository;
import net.toocruel.iqismart.dao.core.ArticleRepository;
import net.toocruel.iqismart.dao.core.CommentRepository;
import net.toocruel.iqismart.dao.core.SupportRepository;
import net.toocruel.iqismart.entity.core.Message;
import net.toocruel.iqismart.entity.setting.Setting;
import net.toocruel.iqismart.service.UserService;
import net.toocruel.iqismart.service.core.*;
import net.toocruel.iqismart.web.controller.base.AbstractBaseController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

@Controller
@RequestMapping("message")
public class MessageController extends AbstractBaseController {

    @Autowired
    private UserService userService;

    @Autowired
    private TagService tagService;

    @Autowired
    private CategoryService categoryService;

    @Autowired
    private QuestionService questionService;

    @Autowired
    private CommentService commentService;


    @Autowired
    private SupportRepository supportRepository;

    @Autowired
    private ArticleRepository articleRepository;

    @Autowired
    private AnswerRepository answerRepository;

    @Autowired
    private CommentRepository commentRepository;

    @Autowired
    private MessageService messageService;

    @GetMapping()
    public ModelAndView index(
            @RequestParam(defaultValue = "20") int size,
            @RequestParam(defaultValue = "1") int page,
            @RequestParam(defaultValue = "id") String sortBy,
            @RequestParam(defaultValue = "desc") String sortOrder){

        ModelAndView modelAndView = new ModelAndView(getTemplate("index"));
        Page<Message> messages = messageService.findALl(getCurrentUser().getId(),
                new PageRequest(page-1,size,new Sort(Sort.Direction.fromString(sortOrder),sortBy)));

        modelAndView.addObject("messages",messages);
        modelAndView.addObject("seoTitle","我的私信 - "+ Setting.SETTING.getWebsite().getName());
        return modelAndView;
    }




    @Override
    protected String getModule() {
        return "message";
    }
}
